package com.ruoyi.comments.service.impl;

import java.util.List;
import java.util.Optional;

import com.ruoyi.comments.domain.Text;
import com.ruoyi.comments.mapper.TextMapper;
import com.ruoyi.comments.mapper.TextMapper;
import com.ruoyi.comments.service.TextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comments.mapper.CommentMapper;
import com.ruoyi.comments.domain.Comment;
import com.ruoyi.comments.service.ICommentService;

/**
 * 评论Service业务层处理
 * 
 * @author madeline
 * @date 2025-05-18
 */
@Service
public class CommentServiceImpl implements ICommentService 
{
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private TextMapper textMapper;
    /**
     * 查询评论
     * 
     * @param id 评论主键
     * @return 评论
     */
    @Override
    public Comment selectCommentById(Long id)
    {
        Comment comment = commentMapper.selectCommentById(id);
        System.err.println(comment);
        Optional<Text> byId = textMapper.findById(comment.getId());

//        Content content = byId.get();
//        comment.setText(content.getContent());
        Text text = byId.orElse(new Text());
        comment.setText(text.getText());
        return comment;
    }

    /**
     * 查询评论列表
     * 
     * @param comment 评论
     * @return 评论
     */
    @Override
    public List<Comment> selectCommentList(Comment comment)
    {
        List<Comment> comments = commentMapper.selectCommentList(comment);
        for (Comment comment1 : comments) {
            Long id = comment1.getId();
//            Content content = contentMapper.findById(id).get();
            Optional<Text> byId = textMapper.findById(id);
            Text text = byId.orElse(new Text());
            comment1.setText(text.getText());
        }
        return comments;
    }

    /**
     * 新增评论
     * 
     * @param comment 评论
     * @return 结果
     */
    @Override
    public int insertComment(Comment comment)
    {
        int i = commentMapper.insertComment(comment);
        if(i>0){
            Text text = new Text();
            text.setId(comment.getId());
            text.setText(comment.getText());
            textMapper.insert(text);
        }
        return i;
    }

    /**
     * 修改评论
     * 
     * @param comment 评论
     * @return 结果
     */
    @Override
    public int updateComment(Comment comment)
    {
        int i = commentMapper.updateComment(comment);
        if(i>0){
            Text text = new Text();
            text.setId(comment.getId());
            text.setText(comment.getText());
            textMapper.save(text);
        }
        return i;
    }

    /**
     * 批量删除评论
     * 
     * @param ids 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentByIds(Long[] ids)
    {
        int i = commentMapper.deleteCommentByIds(ids);
        if(i>0){
            for (Long id : ids) {
                textMapper.deleteById(id);
            }
        }
        return i;
    }

    /**
     * 删除评论信息
     * 
     * @param id 评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentById(Long id)
    {
        int i = commentMapper.deleteCommentById(id);
        if(i>0){
            textMapper.deleteById(id);
        }
        return i;
    }
}
